package com.txMsg.server.controller;

import com.txMsg.client.entity.TxMsgEntity;
import com.txMsg.server.entity.TxMsg;
import com.txMsg.server.service.TxMsgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "TxMsgController")
@Slf4j
public class TxMsgController {
    @Autowired
    TxMsgService txMsgService;
    @ApiOperation(value = "准备存储-事务消息")
    @RequestMapping(value = "prepareTxMsg",method = {RequestMethod.POST})
    public Object prepareTxMsg(@ApiParam(name = "TxMsgEntity", value = "消息-JSON格式", required = true)@RequestBody(required = false) TxMsgEntity txMsgEntity){
        log.info("txMsgEntity - {}",txMsgEntity);
        TxMsg txMsg = new TxMsg();
        BeanUtils.copyProperties(txMsgEntity,txMsg);
        log.info("txMsg - {}",txMsg);
        if(txMsgService.saveTxMsg(txMsg)){
            return "成功";
        }else{
            return "失败";
        }
    }
    @ApiOperation(value = "确认-事务消息")
    @RequestMapping(value = "confirmTxMsg",method = {RequestMethod.POST})
    public Object confirmTxMsg(@ApiParam(name = "txMsgId", value = "消息事务ID", required = true)@RequestParam("txMsgId") String txMsgId){
        return txMsgService.confirmTxMsg(txMsgId);
    }
    @ApiOperation(value = "删除-事务消息")
    @RequestMapping(value = "deleteTxMsg",method = {RequestMethod.POST})
    public Object deleteTxMsg(@ApiParam(name = "txMsgId", value = "消息事务ID", required = true)@RequestParam("txMsgId") String txMsgId){
        return txMsgService.deleteTxMsg(txMsgId);
    }
    @ApiOperation(value = "重新发送状态是【已发送】的数据到MQ")
    @RequestMapping(value = "sendConfirmTxMsg",method = {RequestMethod.POST})
    public Object sendConfirmTxMsg(){
        txMsgService.sendConfirmTxMsg();
        return "sendConfirmTxMsg调用成功";
    }
    @ApiOperation(value = "回调查询-处理 状态是【准备发送】的数据")
    @RequestMapping(value = "callbackTxMsg",method = {RequestMethod.POST})
    public Object callbackTxMsg(){
        txMsgService.callbackTxMsg();
        return "callbackTxMsg调用成功";
    }
}
